package com.fit.toysShop.beans;

import java.util.*;

import com.fit.toysShop.asobject.PurchaseItemBasketAS;
import com.fit.toysShop.interfaces.Product;
import com.fit.toysShop.interfaces.Purchase;
import com.fit.toysShop.interfaces.PurchaseItem;
import com.fit.toysShop.interfaces.Seller;
import com.fit.toysShop.interfaces.Customer;
import com.fit.toysShop.service.PurchaseItems;
import com.fit.toysShop.service.Purchases;
import com.fit.toysShop.service.PurchasesUniqueId;

public class PurchaseBean implements Purchase {
	private String id;
	private String name;
	//private List<Product> products = new LinkedList<Product>();
	private List<PurchaseItem> purchaseItem = new LinkedList<PurchaseItem>();
	private Customer customer;
	private Seller seller;
	private double total;

	public double getTotal() {
		return total;
	}

	public void setTotal(double total) {
		this.total = total;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

//	public List<Product> getProducts() {
//		return products;
//	}
//
//	public void setProducts(List<Product> products) {
//		this.products = products;
//	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	public Seller getSeller() {
		return seller;
	}

	public void setSeller(Seller seller) {
		this.seller = seller;
	}
	
	public List<PurchaseItem> getPurchaseItem() {
		return purchaseItem;
	}

	public void setPurchaseItem(List<PurchaseItem> purchaseItem) {
		this.purchaseItem = purchaseItem;
	}

	public void executePurchaseCreation(Customer customer, Seller seller, PurchaseItemBasketAS purchaseItemBasketAS, PurchaseItems purchaseItems, Purchases purchases) {
		String purchaseId = PurchasesUniqueId.getNextPurchaseId();

		this.setId(purchaseId);
		this.setCustomer(customer);
		this.setSeller(seller);
		this.setPurchaseItem(new LinkedList<PurchaseItem>());

		int no = 0;
		double total = 0.00;
		Iterator<PurchaseItem> purIter = purchaseItemBasketAS.getPurchaseItemBasket().values().iterator();
		while(purIter.hasNext()) {
			
			PurchaseItem currentPI = purIter.next();
			total = total + currentPI.getTotalPrice();
			no = no + 1;
			currentPI.setId(purchaseId + "-" + no);
			currentPI.setNumber(no);
			currentPI.setPurchase(this);			
			this.getPurchaseItem().add(currentPI);
			currentPI.getProduct().getPurchaseItem().add(currentPI);
			purchaseItems.save(currentPI);
		}
		this.setTotal(total);
		customer.getPurchases().add(this);
		seller.getPurchases().add(this);
		purchases.save(this);		
	}

}
